#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#include <inttypes.h>
#include <malloc.h>

int8_t mul(int8_t muland, int8_t mul);
int8_t ddiv(int8_t divend, int8_t div);
int main()
{
	int8_t* x=(int8_t*)malloc(sizeof(int8_t));
	int8_t* y=(int8_t*)malloc(sizeof(int8_t));
	printf("Please enter multiplicand/dividend: ");
	scanf("%"SCNd8,x);
	printf("Please enter multiplier/divisor: ");
	fflush(stdin);
	scanf("%"SCNd8,y);
	printf("The mul result is %"SCNd8"\n",mul(*x,*y));
	printf("The div result is %"SCNd8"\n",ddiv(*x,*y));
    return 0;
}

int8_t mul(int8_t x, int8_t y)
{
	bool sign = ((x^y)>>7);
	int8_t res = 0;
	int8_t muland = abs(x);
	int8_t mul = abs(y);

	int i;
	for(i=8; i>=0; i--){
		if((1<<i)<=mul){
			res = res + muland << i;
			mul = mul - (1<<i);
		}
	}
	return sign ? -res : res;
}

int8_t ddiv(int8_t x, int8_t y)
{
	bool sign = ((x^y)>>7);
	int8_t res = 0;
	int8_t divend = abs(x);
	int8_t div = abs(y);

	int i;
	for(i=8; i>=0; i--){
		if((divend>>i)>=div){
			res = res + (1<<i);
			divend = divend -(div<<i);
		}
	}
	return sign ? -res : res;
}
